import {toast,getUrlParam} from '@/common/public.js'
export const mixins = {
	data() {
		return {
			appId:'wxa55214a2a5ecdcb7',
			// openID:'oovO-55A6-BS9TMC35mZzD2qUIxI',
			// code:'061pU3000AJ5eM1LNt300Rr3Cl0pU30w',
			code:'',
			openID:'',
		}
	},
	methods:{
		// 微信授权
		wxAuth() {
			let code = getUrlParam('code');
			if (!code) {
				this.wxjump(window.location.href)
			} else {
				uni.showLoading({
				  title: '加载中...'
				});
				this.code = code
				this.wxGetOpenId()
			}
		},
		wxjump(fromurl) {
			let redirect_uri = encodeURIComponent(fromurl)
			// let redirect_uri = encodeURIComponent('https://test.sgsmart-online.com')
			var url="https://open.weixin.qq.com/connect/oauth2/authorize?appid="+this.appId+"&redirect_uri="+redirect_uri+"&response_type=code&scope=snsapi_base&state=123#wechat_redirect";
			location.href=url;
		},
		// 微信获取openID
		wxGetOpenId() {
			let param = {
				code:this.code,
				appId:this.appId
			};
			this.$api.getOpenIdByWechatCodeAndAppId(param).then(res=>{
				if (res.code == 200) {
					this.openID = res.data
					this.selectBindingStatusByWUserIdAndAppId()
				}
			})
		},
		selectBindingStatusByWUserIdAndAppId() {
			let param = {
				appId:this.appId,
				wUserId:this.openID
			}
			this.$api.selectBindingStatusByWUserIdAndAppId(param).then(res=>{
				uni.hideLoading()
				if (res.code == 200 && res.success) {
					uni.setStorageSync('sgs_userId',this.openID)
					if (this.entry == 1) {
						uni.redirectTo({
							url:'/pages/bindResult/bindResult'
						})
					} else {
						this.initData()
					}
				} else {
					if (this.entry == 1) {
						this.status = true
					} else {
						uni.redirectTo({
							url:'/pages/login/login'
						})
					}
				}
			})
		},
	}
}